<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Manager extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model("productmodel", "ProductModel");
    }

    function index()
    {
        $this->load->model("productmodel", "ProductModel");
        $data['mComs'] = $this->ProductModel->getMComList();
        $this->renderView('manager/input_product', $data);
    }
    
    public function saveHang()
    {
        if ($this->input->post()) {
            $name = $this->input->post('name', true);
            if (null != $name && trim($name) != '') {
                $this->load->model("productmodel", "ProductModel");
                $p = $this->ProductModel->getMComByName(trim($name));
                if (count($p) >0) {
                    echo "<span class='error'>Insert fail. Hãng may be already exists.</span>";
                }else{
                    $result = $this->ProductModel->saveMCom(trim($name));
                    if ($result > 0) {
                        echo 1;
                    } else {
                        echo "<span class='error'>Insert fail. Hãng may be already exists.</span>";
                    }
                }
            }else {
                echo "<span class='error'><strong>Hãng</strong> can not be blank.</span>";
            }
            return;
        }
        show_404();
    }
    
    public function saveProduct()
    {
        if ($this->input->post()) {
            $data = $this->input->post();
            if (null != $data['name'] && trim($data['name']) != '') {
                $this->load->model("productmodel", "ProductModel");
                $p = $this->ProductModel->getProductByName(trim($data['name']));
                if (count($p) >0) {
                    echo "<span class='error'>Insert fail. Product may be already exists.</span>";
                }else{
                    $result = $this->ProductModel->saveProduct(trim($data['name']),trim($data['hangId']));
                    if ($result > 0) {
                        echo 1;
                    } else {
                        echo "<span class='error'>Insert fail. Product may be already exists.</span>";
                    }
                }
            }else {
                echo "<span class='error'><strong>Product</strong> can not be blank.</span>";
            }
            return;
        }
        show_404();
    }

    public function saveProductDetail()
    {
        if ($this->input->post()) {
            $session_data = $this->session->userdata('logged_in');

            $data = $this->input->post();
            $data['update_user_id'] = $session_data['id'];
            if ((int)$data['product_id'] < 1) {
                echo "<span class='error'>Làm ơn chọn loại trước !</span>";
            }else{
                $result = $this->ProductModel->saveProductDetail($data);
                if (count($result) == 0) {
                    echo 1;
                } else {
                    echo "<span class='error'>Insert fail.";
                    foreach ($result as $key => $value) {
                        echo "<br/>";
                        echo $value;
                    }
                    echo "</span>";
                }
            }
            return;
        }
        show_404();
    }

    public function getProductList()
    {
        $hangId = $this->input->post('hangId', 1);
        $this->load->model("productmodel", "ProductModel");
        $data['products'] = $this->ProductModel->getProductList($hangId);
        $result = $this->getHtmlFromtemplate("common/product_list", $data);
        echo $result;
        return;
    }

    
    public function getMComList()
    {
        $this->load->model("productmodel", "ProductModel");
        $data['mComs'] = $this->ProductModel->getMComList();
        $result = $this->getHtmlFromtemplate("common/mobile_com_list", $data);
        echo $result;
        return;
    }
}
?>